/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.ufba.lasid.mda.servlet;

import br.ufba.lasid.mda.clients.fd.JiTFailureDetectorService;
import br.ufba.lasid.mda.clients.fd.JiTFailureDetectorService_Service;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author cloud15
 */
public class FailureDetectorServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        writeHeader(response);
        writeForm(request, response);
        writeFooter(response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        writeHeader(response);
        writeForm(request, response);
        PrintWriter pw = response.getWriter();
        pw.println("<BR>");

        JiTFailureDetectorService_Service wsFailure = new JiTFailureDetectorService_Service();
        JiTFailureDetectorService failureDetector = wsFailure.getJiTFailureDetectorServicePort();

        String first = request.getParameter("FIRSTVALUE");
        String ret = failureDetector.hello(first);
        pw.println("Hello: " + ret);
        pw.println("<BR>");

        String second = request.getParameter("SECONDVALUE");
        ret = failureDetector.register(second, true);
        pw.println("Register (" + second + ") = " + ret);
        pw.println("<BR>");

        ret = failureDetector.buildup();
        pw.println("Buildup = " + ret);
        pw.println("<BR>");

        String third = request.getParameter("THIRDVALUE");
        try {
            ret = failureDetector.setPeriod(Long.parseLong(third));
            pw.println("Period (" + third + ") = " + ret);
            pw.println("<BR>");
        } catch (NumberFormatException nfe) {
            pw.println("wrong Period!");
            pw.println("<BR>");
        }

        String four = request.getParameter("FOURVALUE");
        try {
            ret = failureDetector.setTimeout(Long.parseLong(four));
            pw.println("Timeout (" + four + ") = " + ret);
            pw.println("<BR>");
        } catch (NumberFormatException nfe) {
            pw.println("wrong Timeout!");
            pw.println("<BR>");
        }

        ret = failureDetector.startup();
        pw.println("Startup = " + ret);
        pw.println("<BR>");

        writeFooter(response);
    }

    private final void writeForm(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        String first = request.getParameter("FIRSTVALUE");
        String second = request.getParameter("SECONDVALUE");
        String third = request.getParameter("THIRDVALUE");
        String four = request.getParameter("FOURVALUE");

        PrintWriter pw = response.getWriter();
        pw.println("<FORM ACTION=\"FailureDetectorServlet\" METHOD=\"POST\">");
        pw.println("Hello: ");
        pw.println("<INPUT TYPE=\"TEXT\" NAME=\"FIRSTVALUE\" VALUE=\"" + ((first != null) ? first : "") + "\"/>");
        pw.println("<BR>");

        pw.println("Register: ");
        pw.println("<INPUT TYPE=\"TEXT\" NAME=\"SECONDVALUE\" VALUE=\"" + ((second != null) ? second : "") + "\"/>");
        pw.println("<BR>");

        pw.println("Period: ");
        pw.println("<INPUT TYPE=\"TEXT\" NAME=\"THIRDVALUE\" VALUE=\"" + ((third != null) ? third : "") + "\"/>");
        pw.println("<BR>");

        pw.println("Timeup: ");
        pw.println("<INPUT TYPE=\"TEXT\" NAME=\"FOURVALUE\" VALUE=\"" + ((four != null) ? four : "") + "\"/>");
        pw.println("<BR>");

        pw.println("<BR>");
        pw.println("<INPUT TYPE=\"SUBMIT\" VALUE=\"SUBMIT\"/>");
        pw.println("</FORM>");
    }

    private final void writeHeader(HttpServletResponse response) throws IOException {
        response.getWriter().println("<HTML><TITLE>Failure Detector Service</TITLE><BODY>");
        response.getWriter().println("<h1>Failure Detector Service</h1>");
    }

    private final void writeFooter(HttpServletResponse response) throws IOException {
        response.getWriter().println("<a href=\'../jitclouds-mda-web-services\'>index</a>");
        response.getWriter().println("</BODY></HTML>");
    }
}
